package com.slz3.business.module.goodsManagement.gooBaseGoodsAttribute.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttribute.domain.GooBaseGoodsAttribute;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttribute.domain.GooBaseGoodsAttributeFactory;
import com.slz3.business.module.goodsManagement.gooBaseGoodsAttribute.steward.GooBaseGoodsAttributeSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class GooBaseGoodsAttributeServiceImpl implements GooBaseGoodsAttributeService {
    @Override
    public GooBaseGoodsAttribute save(GooBaseGoodsAttribute gooBaseGoodsAttribute) {
        return Repositorys.gooBaseGoodsAttribute.save(GooBaseGoodsAttributeFactory.initBaseFields(gooBaseGoodsAttribute));
    }
    @Override
    public Page<GooBaseGoodsAttribute> page(MultiValueMap<String, String> params) {
        Page<GooBaseGoodsAttribute> gooBaseGoodsAttributePage = Repositorys.gooBaseGoodsAttribute.findAll(
                GooBaseGoodsAttributeSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return gooBaseGoodsAttributePage;
    }


    @Override
    public GooBaseGoodsAttribute modify(GooBaseGoodsAttribute gooBaseGoodsAttributeNew) {
        return Repositorys.gooBaseGoodsAttribute.saveAndFlush(GooBaseGoodsAttributeFactory.initBaseFields(gooBaseGoodsAttributeNew));
    }

    @Override
    public Optional<GooBaseGoodsAttribute> single(UUID uuid) {
        return Repositorys.gooBaseGoodsAttribute.findById(uuid);
    }

    @Override
    public List<GooBaseGoodsAttribute> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooBaseGoodsAttribute.findAllById(uuids);
    }

    @Override
    public List<GooBaseGoodsAttribute> delete(List<GooBaseGoodsAttribute> gooBaseGoodsAttributeList) {
        gooBaseGoodsAttributeList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooBaseGoodsAttributeFactory.initBaseFields(item);
        });
        return Services.gooBaseGoodsAttributeTransactional.batchModify(gooBaseGoodsAttributeList);
    }

}
